<template>
    <my-input :id="id"  :styles="styles"  :sign="sign" :time="time"  :valsql="valsql" :index="index" isChange="Y"  :data="data" :exkey="exkey" @input="changeData"></my-input>
</template>
<script>
import MyInput  from '../input/myInput'
export default {
    props:['id','styles','value','data','index','appid','valsql','sign','time','exkey'],
    name: "input-kio",
    components: {
        MyInput
    },
     data(){
        return{
            oldVal:"",
            typeOld:"",
            typeField:"",
            typeFieldP:"",
            typeFieldI:"",
            kio:'',
            amt:"",
            itemField:'',
            pId:"",
            cId:"",
            sfItemData:[],
            sz_depart:"",
            baseAmt:""
        }
    },
    created(){
        this.sz_depart=this.data["szInfo"].sz_depart;
        this.typeField=this.exkey.typeField;
        this.kio=this.exkey.kio;
        if(this.kio){
            this.kio=Number(this.kio)
        }
        this.amt=this.exkey.amt;
        if(this.amt){
            this.amt=Number(this.amt)
        }
        this.itemField=this.exkey.itemField;

        let ids=this.typeField.split(".");
        this.typeFieldP=ids[0];
        this.typeFieldI=ids[1]

        let idss=this.id.split(".");
        this.pId=idss[0];
        this.cId=idss[1];
        this.typeOld=this.data[this.typeFieldP][this.typeFieldI];
        this.oldVal=this.data[this.pId][this.cId];
        
        this.loadItem();
    },
    watch:{
        data:{
            deep:true,
            handler:function(newV,oldV){
                // alert(this.change+"=="+this.isJm)
               if(this.typeOld!=this.data[this.typeFieldP][this.typeFieldI]){
                    this.typeOld=this.data[this.typeFieldP][this.typeFieldI]
                    for(let i=0;i<this.sfItemData.length;i++){
                        let item=this.sfItemData[i];
                        if(item.szi_ex1==this.typeOld){
                            this.baseAmt=Number(item.szi_ex2);
                        }
                    }
                    this.changeKio();
               }
               if(this.oldVal!=this.data[this.pId][this.cId]){
                    this.oldVal=this.data[this.pId][this.cId];
                    this.changeKio();
               }
            }
        }
    },
    methods:{
        loadItem(){
             this.$post("bygex","byg/funeral/getFuneralSfItem",{depart:this.sz_depart,type2:"车辆"}).then(data=>{
                this.sfItemData=[];
                for(let i=0;i<data.item.length;i++){
                    this.sfItemData.push(data.item[i]);
                }
                for(let i=0;i<this.sfItemData.length;i++){
                    let item=this.sfItemData[i];
                    if(item.szi_ex1==this.typeOld){
                        this.baseAmt=Number(item.szi_ex2);
                    }
                }
            });
        },
        changeKio(){
            let items=this.data[this.itemField];
            for(let i=0;i<items.length;i++){
                if(items[i].szi_ex1==this.data[this.typeFieldP][this.typeFieldI]){
                    let val=this.data[this.pId][this.cId];
                    let amts=0;
                    
                    if(val){
                        val=Number(val);
                        if(val>this.kio){
                            let val_=val-this.kio;
                            amts=this.baseAmt+val_*this.amt;
                           
                        }else{
                            amts=this.baseAmt;
                        }
                        this.data[this.itemField][i].szi_ex2=amts;
                        
                        this.data[this.itemField][i].szi_ex5=amts;
                    }
                   
                }
            }
        },
        changeData(data){
            console.log("selectdata=================="+data)
           
        }
    }
}
</script>